package alpha.com.google.code.mechanic.janitor.controller;

import java.io.IOException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import alpha.com.google.code.mechanic.janitor.model.IContext;

import com.google.eclipse.mechanic.plugin.core.IMechanicPreferences;
import com.google.eclipse.mechanic.plugin.core.MechanicPlugin;

/**
 * 
 * This class is to modify the Task sources of the Workspace Mechanic:
 * (1) remove all the Janitor Contexts directory from the Task source;
 * (2) add a Janitor Context directory to the Task source.
 * 
 * @History:
 * 1. Add a method void checkSelCtxExist (ContextData ctxData) to check
 * whether the directory of the activated Context is still in the task
 * source list
 * @author jingjiejiang
 * @Edited Nov 3, 2014
 * 
 * @Last_edited: Nov 3, 2014
 * 
 * @author jingjiejiang
 * @since Sep 21, 2014
 */
public class MechanicTaskSource {

  private IPreferenceStore prefStore; 
  
  public MechanicTaskSource() {

    prefStore = MechanicPlugin.getDefault().getPreferenceStore();
  }
  
  public String getCurTaskSources () {
    
    return prefStore.getString(IMechanicPreferences.DIRS_PREF);
  }
  
  /**
   * Check whether there already exists the same directory already. If yes,
   * return true;otherwise, return false.
   *
   * @param location
   * @return
   */
  public boolean hasSameLocation (String locations, String newlocation) {
    
    boolean hasSame = false;
    
    if(locations.contains(newlocation)) {
      hasSame = true;
    }
    
    return hasSame;
  }
  
  /**
   * Remove all the Context directories from the Task sources in the Mechanic page.
   */
  public void removeAllCtxDirFromTaskSrc() {
    
    String locations = prefStore.getString(IMechanicPreferences.DIRS_PREF);
    String templocations = "";
    
    if (hasSameLocation(locations, "Contexts")) {
      // Remove the first "[" and the last "]" in the old string
      int index = locations.indexOf("]");
      if (index != -1) {
        locations = locations.substring(1, index);
      } 
      
      String[] dirlist = locations.split(",");
      
      for(String str: dirlist){
        if(!str.contains("Contexts")) {
          templocations = templocations + str + ",";
        }
      }
      
      index = templocations.lastIndexOf(",");
      templocations = templocations.substring(0, index);
      locations = "[" + templocations + "]";
      prefStore.setValue(IMechanicPreferences.DIRS_PREF, locations);
    }
  }
  
  /**
   * Adding a new context's directory to the Task source of Workspace Mechanic.
   * If add the directory successfully, return true;otherwise, return false.
   * 
   * @param location the directory that suppose to add the the preferences
   * @return
   */
  public boolean addDirectoryToTaskSrc (String location) {
    
    boolean isSuccess = false;

    String locations = prefStore.getString(IMechanicPreferences.DIRS_PREF);
    
    if (!hasSameLocation(locations, location)) {
      // Remove the last "]" in the old string, add the new locations to the string
      int index = locations.indexOf("]");
      if (index != -1) {
        locations = locations.substring(0, index) + ",\"" + location + "\"]";
      } else {
        locations = locations + ",\"" + location;
      }
      
      
      prefStore.setValue(IMechanicPreferences.DIRS_PREF, locations);
      isSuccess = true; 
    }
    
    return isSuccess;
  }
  
  /**
   * The method is to check whether the directory of the activated Context is
   * still in the task source list, if not, clear the selection mark in the Context list.
   *
   * @param ctxData
   */
  public void checkSelCtxExist (ContextData ctxData) {
    
    MechanicTaskSource tasks = new MechanicTaskSource();
    String sources = tasks.getCurTaskSources();
    IContext context = ctxData.getContextsCollector().getSelContext();
    if (context != null) {
      if (false == sources.contains(context.getName())) {
        
        Shell pshell = Display.getCurrent().getActiveShell(); 
        MessageDialog.openInformation(pshell, "Information", "The activated "
            + "Context: " + context.getName() + " has been removed from Task "
                + "Source in the Workspace Mechanic Preference Page.");
        ctxData.getContextsCollector().clearContextSelection();
        try {
          ctxData.saveContextsInfo();
        } catch (IOException e) {
          MessageDialog.openError(pshell, "Couldn't save the Contexts information"
              + " to the context.con file.", e.getMessage());
        }
      }
    }
  }
}
